package org.csu.onlineedubackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import org.csu.onlineedubackend.entity.User;
import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Integer id);

    @Select("SELECT * FROM user WHERE username LIKE CONCAT('%', #{keyword}, '%') OR name LIKE CONCAT('%', #{keyword}, '%')")
    List<User> searchByUsernameOrName(String keyword);

    /**
     * 获取每月注册学生数
     * @return 每月注册学生数列表，包含月份和人数
     */
    @Select("SELECT DATE_FORMAT(created_at, '%Y-%m') AS month, COUNT(*) AS registerCount FROM user WHERE role = 1 GROUP BY month ORDER BY month")
    java.util.List<java.util.Map<String, Object>> getMonthlyRegisteredStudents();

    //批量查用户名
    @Select({
            "<script>",
            "SELECT id, name FROM user WHERE id IN",
            "<foreach collection='userIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<User> selectNamesByIds(@Param("userIds") List<Integer> userIds);
    
    /**
     * 通过用户名查询用户
     * @param username 用户名
     * @return 用户对象
     */
    @Select("SELECT * FROM user WHERE username = #{username}")
    User selectByUsername(@Param("username") String username);
}

